package sprinklerbee.views;

import javax.swing.JPanel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;

import sprinklerbee.factory.ServiceFactory;
import sprinklerbee.model.Configuration;
import sprinklerbee.utils.MemoryCache;

import java.awt.GridLayout;
import java.awt.FlowLayout;

@SuppressWarnings("serial")
public class ConfigPanel extends JPanel {

	private JButton btn_save;
	private JButton btn_clear;
	private JTextField tempMin_txt;
	private JTextField tempMax_txt;
	private JTextField water_usage_txt;
	private JPanel panel;
	private JPanel panel_2;
	private JPanel panel_1;
	private JPanel panel_3;
	private JPanel panel_4;
	private JPanel panel_5;

	public ConfigPanel() {

		Map<String, Configuration> map=ServiceFactory.getConfigurationServiceInstance().getConfigurations();
		
		setSize(680, 304);

		setBorder(new TitledBorder("Config Panel"));
		setLayout(new GridLayout(0, 2, 0, 0));
		
		panel_1 = new JPanel();
		add(panel_1);
		
		JLabel lbl_tempMax = new JLabel();
		panel_1.add(lbl_tempMax);
		lbl_tempMax.setText("Temperature Maximun:");
		tempMax_txt = new JTextField(5);
		panel_1.add(tempMax_txt);
		tempMax_txt.setText(map.get("maxtemp").getValue());
		
		panel = new JPanel();
		FlowLayout flowLayout = (FlowLayout) panel.getLayout();
		flowLayout.setAlignment(FlowLayout.LEFT);
		add(panel);
		
				JLabel lbl_tempMin = new JLabel();
				panel.add(lbl_tempMin);
				lbl_tempMin.setText("Temperature Minimum:");
				tempMin_txt = new JTextField(5);
				panel.add(tempMin_txt);
				tempMin_txt.setText(map.get("mintemp").getValue());
		
		panel_2 = new JPanel();
		add(panel_2);
		
				JLabel lbl_water_usage = new JLabel();
				panel_2.add(lbl_water_usage);
				lbl_water_usage.setText("Water flow per hour:");
				water_usage_txt = new JTextField(5);
				panel_2.add(water_usage_txt);
				water_usage_txt.setText(map.get("waterflow").getValue());
						
						panel_4 = new JPanel();
						add(panel_4);
						
						panel_5 = new JPanel();
						add(panel_5);
						
						panel_3 = new JPanel();
						FlowLayout flowLayout_1 = (FlowLayout) panel_3.getLayout();
						flowLayout_1.setAlignment(FlowLayout.LEFT);
						add(panel_3);
						btn_save = new JButton();
						panel_3.add(btn_save);
						btn_save.addActionListener(new ActionListener() {
							public void actionPerformed(ActionEvent e) {
								// Validate input data

								if (validateConfig()){
									try{
										String temp_min = tempMin_txt.getText();
										
										String temp_max = tempMax_txt.getText();
										
										String water_usage = water_usage_txt.getText();
										
										Map<String, String> map=new HashMap<String, String>();
										map.put("waterflow",water_usage);
										map.put("mintemp", temp_min);
										map.put("maxtemp", temp_max);
										if (ServiceFactory.getConfigurationServiceInstance().updateConfigurations(map)) {
											JOptionPane.showMessageDialog(null, "Update successfully!");
											MemoryCache.refreshMemoryCache();
										}
									
									} catch (NumberFormatException e1) {
										JOptionPane.showMessageDialog(null,
												"The input value is not integer!");
									} catch (RuntimeException e2) {
										JOptionPane.showMessageDialog(null,
												"Internal error!");
									}	
								}
					
							}
						});
						btn_save.setText(" Save ");
						btn_save.setToolTipText("Save");
						
								btn_clear = new JButton();
								panel_3.add(btn_clear);
								btn_clear.addActionListener(new ActionListener() {
									public void actionPerformed(ActionEvent e) {
										tempMax_txt.setText("");
										tempMin_txt.setText("");
										water_usage_txt.setText("");
									}
								});
								btn_clear.setText(" Clear ");
								btn_clear.setToolTipText("Clear");
	}
	
	public boolean validateConfig(){
		
		boolean val = true;
		if (tempMax_txt.getText().isEmpty() )
		{
			JOptionPane.showMessageDialog(null, "Maximum tempretaure canot be empty!");
			val = false;
		}
		
		if (tempMin_txt.getText().isEmpty())
		{
			JOptionPane.showMessageDialog(null, "Minimum tempretaure canot be empty!");
			val = false;
		}
		
		if (water_usage_txt.getText().isEmpty())
		{
			JOptionPane.showMessageDialog(null, "Water usage canot be empty!");
			val = false;
		}
		
		if ( Double.parseDouble(tempMin_txt.getText()) >= Double.parseDouble(tempMax_txt.getText()))
		{
			JOptionPane.showMessageDialog(null, "Minimum tempretaure canot be greater than maximum!");
			val = false;
		}
		
		if (Double.parseDouble(tempMax_txt.getText()) > 160)
		{
			JOptionPane.showMessageDialog(null, "Maximum tempretaure canot be greater than 160!");
			val = false;
		}
		
		if (Double.parseDouble(water_usage_txt.getText()) > 160)
		{
			JOptionPane.showMessageDialog(null, "Water usage cannot be empty!");
			val = false;
		}
		return val;
	}
}

